//
//  UserProjectViewController.m
//  XinKaiFa55like
//
//  Created by junseek on 2017/3/24.
//  Copyright © 2017年 55like lj. All rights reserved.
//

#import "UserProjectViewController.h"
#import "RHTableView.h"
#import "UserProjectTableViewCell.h"
#import "SelectWebUrlViewController.h"

@interface UserProjectViewController ()<UISearchBarDelegate>{
    UISearchBar *txtSearch;
    
    RHTableView  *table_user;
}

@end

@implementation UserProjectViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    txtSearch.text=@"";
    //注册监听键盘事件
    RegisterNotify(UIKeyboardWillShowNotification, @selector(handleKeyboardDidShow:))
    RegisterNotify(UIKeyboardWillHideNotification, @selector(handleKeyboardWillHide:))
    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [table_user cancelDownload];
    RemoveNofify
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    txtSearch= [[UISearchBar alloc] initWithFrame:CGRectMake(0, kTopHeight, kScreenWidth, 44)];
    txtSearch.delegate=self;
    txtSearch.placeholder=@"搜索";
    [self.view addSubview:txtSearch];
    [txtSearch setBackgroundImage:[[Utility Share] imageFromColor:rgbGray rect:txtSearch.frame.size]];
    
    [self initTabelViews];
    [self loadTableData];
}
-(void)initTabelViews{
    table_user=[[RHTableView alloc]initWithFrame:CGRectMake(0, kTopHeight+44, kScreenWidth, kContentHeight-44)];
    [table_user showRefresh:YES LoadMore:YES];
    table_user.delegate = self;
    table_user.dataSource = self;
    [table_user setBackgroundColor:rgbGray];
    [table_user setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:table_user];
    [table_user setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
    [table_user registerClass:[UserProjectTableViewCell class] forCellReuseIdentifier:@"UserProjectTableViewCell"];
}
-(void)loadTableData{
    krequestParam
    [dictparam setValue:@"%@" forKey:@"page"];
    [dictparam setValue:@"20" forKey:@"pagesize"];
    [dictparam setValue:txtSearch.text forKey:@"keys"];
    table_user.urlString=[NSString stringWithFormat:@"%@%@",XKFV3usermyproject,dictparam.wgetParamStr];
    [table_user refresh];
}
#pragma mark Keyboard
-(void)handleKeyboardDidShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
    
    [UIView animateWithDuration:0.3 animations:^{
        
    } completion:^(BOOL finished) {
        
    }];
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        
    }completion:^(BOOL finished) {
        
    }];
}

#pragma mark tableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return table_user.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];
    UserProjectTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UserProjectTableViewCell" forIndexPath:indexPath];
    [cell setValueForDictionary:dic indexPath:indexPath type:self.userInfo];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=[table_user.dataArray objectAtIndex:indexPath.row];
    [self pushController:[SelectWebUrlViewController class] withInfo:@"userProject" withTitle:@"" withOther:dic];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{    
    [self.view endEditing:YES];
}
#pragma mark UISearchBarDelegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    [self loadTableData];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [self loadTableData];
    [searchBar resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
